<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/11
 * Time: 10:25
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Goods extends Base
{
    /**
     * 商品列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //商品名称
            if(isset($params['good_name']) && $params['good_name'] != ''){
                $where['g.good_name|good_id'] = ['like', '%' . $params['good_name'] . '%'];
            }
            
            //商品类型
            if(isset($params['good_type']) && $params['good_type'] != ''){
                $where['good_type'] = $params['good_type'];
            }
            
            //状态
            if(isset($params['good_status']) && $params['good_status'] != ''){
                $where['good_status'] = $params['good_status'];
            }
            if(isset($params['good_hot']) && $params['good_hot'] != ''){
                $where['good_hot'] = $params['good_hot'];
            }
            //所属类目
            if(isset($params['good_assortment']) && $params['good_assortment'] != ''){
                $where['good_assortment'] = $params['good_assortment'];
            }
            
            //年级
            if(isset($params['grade']) && $params['grade'] != ''){
                $where['good_grade_id'] = $params['grade'];
            }
            //分类
            if(isset($params['type']) && $params['type'] != ''){
                $where['good_type_id'] = $params['type'];
            }
            //出版社
            if(isset($params['press']) && $params['press'] != ''){
                $where['good_press_id'] = $params['press'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods')
                ->alias('g')
                ->join('up_goods_assortment', 'assortment_id = good_assortment', 'left')
                ->join('up_good_grade ugg', 'ugg.good_name_id = good_grade_id', 'left')
                ->join('up_good_type ugt', 'ugt.good_name_id = good_type_id', 'left')
                ->join('up_good_press ugp', 'ugp.good_name_id = good_press_id', 'left')
                ->where($where)->count();
            //用于全部导出
            $count2 = Db::name('goods')
                ->alias('g')
                ->join('up_goods_assortment', 'assortment_id = good_assortment', 'left')
                ->join('up_good_grade ugg', 'ugg.good_name_id = good_grade_id', 'left')
                ->join('up_good_type ugt', 'ugt.good_name_id = good_type_id', 'left')
                ->join('up_good_press ugp', 'ugp.good_name_id = good_press_id', 'left')
                ->where($where)
                ->column('good_id');// 获取总条数
            $lists = Db::name('goods')
                ->alias('g')
                ->join('up_goods_assortment', 'assortment_id = good_assortment', 'left')
                ->join('up_good_grade ugg', 'ugg.good_name_id = good_grade_id', 'left')
                ->join('up_good_type ugt', 'ugt.good_name_id = good_type_id', 'left')
                ->join('up_good_press ugp', 'ugp.good_name_id = good_press_id', 'left')
                ->field('good_id, good_create_time, g.good_name as good_name, good_type, assortment_name, 
                good_y_price, good_stock, good_weight, good_status, good_sales_num, 
                good_visitor, good_cost, good_price, good_image, ugg.good_name as good_grade, 
                ugt.good_name as good_cate, ugp.good_name as good_press, good_thumb,good_hot')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('good_create_time desc')
                ->select();
            
            foreach($lists as $k => $v){
                if($v['good_type'] == 1){
                    //非书籍库存
                    $good_stock = Db::table('up_good_spec')
                        ->where('spec_good_id', $v['good_id'])
                        ->sum('spec_stock');
                    $lists[$k]['good_stock'] = $good_stock;
                    //非书籍价格
                    $good_price = Db::table('up_good_spec')
                        ->where('spec_good_id', $v['good_id'])
                        ->field('spec_price')
                        ->find();
                    $lists[$k]['good_y_price'] = $good_price['spec_price'];
                }
                //图片集
//                if($v['good_image'] != ''){
//                    $complain_img = trim($v['good_image'],',');
//                    $image = explode (',',$complain_img);
//                    $lists[$k]['picture'] = $image[0];
//                    unset($image[0]);
//                    $lists[$k]['good_image'] = array_values($image);
//                }
            }
            $str = '';
            if($count2){
                $str = implode($count2,',');
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'count2' => $str, 'data' => $lists]);
        }
        //商品类型
        $goods_cate = Db::table('up_goods_assortment')
            ->where('assortment_status', 1)
            ->where('assortment_del', 1)
            ->field('assortment_id, assortment_name')
            ->select();
        //出版社
        $good_press = Db::table('up_good_press')
            ->field('good_name_id, good_name')
            ->select();
        //年级
        $good_grade = Db::table('up_good_grade')
            ->field('good_name_id, good_name')
            ->select();
        //分类
        $good_type = Db::table('up_good_type')
            ->field('good_name_id, good_name')
            ->select();
        $this->assign([
            'cate' => $goods_cate,
            'press' => $good_press,
            'grade' => $good_grade,
            'type' => $good_type
        ]);
        if(session('?good_id')){
            session('good_id', null);
        }
        return $this->fetch();
    }
    
    /**
     * 添加图书
     * @return mixed|\think\response\Json
     */
    public function bookAdd(){
        if(Request::instance()->isPost()){
            $good_name = trim(input('good_name'), ' ');
            $name_len = mb_strlen($good_name, 'utf-8');
            $good_y_price = trim(input('good_y_price'), ' ');
            $good_price = trim(input('good_price'), ' ');
            if($name_len > 60){
                return json(['code' => 100, 'msg' => '商品名不能超过60字']);
            }
            if(empty(input('photo'))){
                return json(['code' => 100, 'msg' => '请上传图片']);
            }
            if($good_y_price <= 0){
                return json(['code' => 100, 'msg' => '金币必须大于0']);
            }
            if(mb_strlen($good_price, 'utf-8') > 6){
                return json(['code' => 100, 'msg' => '折扣价不可超过6个字']);
            }
//            $good_thumb = explode(',', input('photo'));
            $data = [
                'good_name' => trim(input('good_name'), ' '),//图书名称
                'good_price' => $good_price,//折扣价
                'good_thumb' => input('param.img1'),//展示图片
                'good_create_time' => date('Y-m-d H:i:s'),//创建时间
                'good_y_price' => $good_y_price,//金币
                'good_author' => trim(input('good_author'), ' '),//作者
                'good_press' => trim(input('good_press'), ' '),//出版社
                'good_isbn' => trim(input('good_isbn'), ' '),//ISBN
                'good_assortment' => input('good_assortment'),//分类
                'good_content' => input('good_content'),//图文详情
                'good_weight' => trim(input('good_weight'), ' '),//重量
                'good_intro' => trim(input('good_intro'), ' '),//简介
                'good_type' => 2,//图书
                'good_stock' => trim(input('good_stock'), ' '),//库存
                'good_image' => input('photo'),//图片
                'good_yi' => trim(input('good_yi'), ' '),//编译者
                'good_grade_id' => trim(input('good_grade_id'), ' '),//年级
                'good_type_id' => trim(input('good_type_id'), ' '),//分类
                'good_press_id' => trim(input('good_press_id'), ' ')//出版社
            ];
            $res = Db::table('up_goods')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_goods_assortment')
            ->where('assortment_status', 1)
            ->where('assortment_del', 1)
            ->where('assortment_type', 2)
            ->field('assortment_id, assortment_name')
            ->select();
        $data1 = Db::table('up_good_grade')->select();//年级
        $data2 = Db::table('up_good_press')->select();//出版社
        $data3 = Db::table('up_good_type')->select();//类型
        $this->assign([
            'cate' => $cate
        ]);
        $this->assign('data1',$data1);
        $this->assign('data2',$data2);
        $this->assign('data3',$data3);
        return $this->fetch('goods/bookadd');
    }
    
    /**
     * 添加非图书
     * @return mixed|\think\response\Json
     */
    public function unbookAdd(){
        if(Request::instance()->isPost()){
            $good_name = trim(input('good_name'), ' ');
            $name_len = mb_strlen($good_name, 'utf-8');
            $good_cost = trim(input('good_cost'), ' ');
            if($name_len > 60){
                return json(['code' => 100, 'msg' => '商品名不能超过60字']);
            }
            if(empty(input('photo'))){
                return json(['code' => 100, 'msg' => '请上传图片']);
            }
            if($good_cost <= 0){
                return json(['code' => 100, 'msg' => '成本价必须大于0']);
            }
            
//            $good_thumb = explode(',', input('photo'));
//            'good_thumb' => input('param.img1'),//展示图片

            $data = [
                'good_name' => $good_name,//图书名称
                'good_assortment' => input('good_assortment'),//分类
                'good_cost' => $good_cost,//成本价
                'good_thumb' => input('param.img1'),//展示图片
                'good_image' => input('photo'),//图片
                'good_create_time' => date('Y-m-d H:i:s'),//创建时间
                'good_weight' => trim(input('good_weight'), ' '),//重量
                'good_content' => input('good_content'),//图文详情
                'good_type' => 1,//非图书
                'good_grade_id' => trim(input('good_grade_id'), ' '),//年级
                'good_type_id' => trim(input('good_type_id'), ' '),//分类
                'good_press_id' => trim(input('good_press_id'), ' ')//出版社
            ];
            
            $res = Db::table('up_goods')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_goods_assortment')
            ->where('assortment_status', 1)
            ->where('assortment_del', 1)
            ->where('assortment_type', 1)
            ->field('assortment_id, assortment_name')
            ->select();
        $this->assign([
            'cate' => $cate
        ]);
        $data1 = Db::table('up_good_grade')->select();//年级
        $data2 = Db::table('up_good_press')->select();//出版社
        $data3 = Db::table('up_good_type')->select();//类型
        $this->assign('data1',$data1);
        $this->assign('data2',$data2);
        $this->assign('data3',$data3);
        return $this->fetch('goods/unbookadd');
    }
    
    /**
     * 非图书规格列表
     * @return mixed|\think\response\Json
     */
    public function specIndex(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['spec_good_id'] = session('good_id');
            $count = Db::name('good_spec')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('good_spec')
                ->where($where)
                ->field('spec_id, spec_name, spec_stock, spec_price')
                ->page($Nowpage,$limits)
                ->order('spec_id desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $good_id = input('good_id');
        if($good_id != ''){
            session('good_id', $good_id);
        }
        return $this->fetch('goods/specindex');
    }
    
    /**
     * 非图书规格添加
     * @return mixed|\think\response\Json
     */
    public function specAdd(){
        if(Request::instance()->isPost()){
            $spec_name = trim(input('spec_name'), ' ');
            $spec_stock = trim(input('spec_stock'), ' ');
            $spec_price = trim(input('spec_price'), ' ');
            Db::startTrans();
            try{
                //添加到规格表
                $data = [
                    'spec_name' => $spec_name,
                    'spec_stock' => $spec_stock,
                    'spec_price' => $spec_price,
                    'spec_good_id' => session('good_id')
                ];
                Db::table('up_good_spec')->insert($data);
                //更新商品表库存
                Db::table('up_goods')->where('good_id', session('good_id'))->setInc('good_stock', $spec_stock);
                Db::commit();
                $res = 1;
            }catch(\Exception $e){
                Db::rollback();
                $res = 0;
            }
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('goods/specadd');
    }
    
    /**
     * 非图书规格编辑
     * @return mixed|\think\response\Json
     */
    public function specEdit(){
        $spec_id = input('spec_id');
        if(Request::instance()->isPost()){
            $spec_name = trim(input('spec_name'), ' ');
            $spec_stock = trim(input('spec_stock'), ' ');
            $spec_price = trim(input('spec_price'), ' ');
            Db::startTrans();
            try{
                //更新规格表
                $update = [
                    'spec_name' => $spec_name,
                    'spec_stock' => $spec_stock,
                    'spec_price' => $spec_price
                ];
                Db::table('up_good_spec')->where('spec_id', $spec_id)->update($update);
                //更新商品表库存
                $stock = Db::table('up_good_spec')->where('spec_good_id', session('good_id'))->sum('spec_stock');
                Db::table('up_goods')->where('good_id', session('good_id'))->update(['good_stock' => $stock]);
                Db::commit();
                $res = 1;
            }catch(\Exception $e){
                Db::rollback();
                $res = 0;
            }
            
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $spec = Db::table('up_good_spec')
            ->field('spec_name, spec_stock, spec_price')
            ->where('spec_id', $spec_id)
            ->find();
        $this->assign([
            'spec_id' => $spec_id,
            'spec' => $spec
        ]);
        return $this->fetch('goods/specedit');
    }
    
    /**
     * 非图书规格删除
     * @return mixed|\think\response\Json
     */
    public function specDel(){
        $spec_id = input('id');
        Db::startTrans();
        try{
            //删除规格
            Db::table('up_good_spec')->where('spec_id', $spec_id)->delete();
            //更新商品表库存
            $stock = Db::table('up_good_spec')->where('spec_good_id', session('good_id'))->sum('spec_stock');
            Db::table('up_goods')->where('good_id', session('good_id'))->update(['good_stock' => $stock]);
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 0;
        }
        
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 非图书参数列表
     * @return mixed|\think\response\Json
     */
    public function paramIndex(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $where['parameter_good_id'] = session('good_id');
            $count = Db::name('good_parameter')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('good_parameter')
                ->where($where)
                ->field('parameter_id, parameter_name, parameter_value')
                ->page($Nowpage,$limits)
                ->order('parameter_id desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $good_id = input('good_id');
        if($good_id != ''){
            session('good_id', $good_id);
        }
        return $this->fetch('goods/paramindex');
    }
    
    /**
     * 非图书参数添加
     * @return mixed|\think\response\Json
     */
    public function paramAdd(){
        if(Request::instance()->isPost()){
            $parameter_name = trim(input('parameter_name'), ' ');
            $parameter_value = trim(input('parameter_value'), ' ');
            //添加到规格表
            $data = [
                'parameter_name' => $parameter_name,
                'parameter_value' => $parameter_value,
                'parameter_good_id' => session('good_id')
            ];
            $res = Db::table('up_good_parameter')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('goods/paramadd');
    }
    
    /**
     * 非图书参数编辑
     * @return mixed|\think\response\Json
     */
    public function paramEdit(){
        $param_id = input('param_id');
        if(Request::instance()->isPost()){
            $parameter_name = trim(input('parameter_name'), ' ');
            $parameter_value = trim(input('parameter_value'), ' ');
            //更新规格表
            $update = [
                'parameter_name' => $parameter_name,
                'parameter_value' => $parameter_value,
            ];
            $res = Db::table('up_good_parameter')->where('parameter_id', $param_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $param = Db::table('up_good_parameter')
            ->field('parameter_name, parameter_value')
            ->where('parameter_id', $param_id)
            ->find();
        $this->assign([
            'parameter_id' => $param_id,
            'param' => $param
        ]);
        return $this->fetch('goods/paramedit');
    }
    
    /**
     * 非图书参数删除
     * @return mixed|\think\response\Json
     */
    public function paramDel(){
        $param_id = input('id');
        $res = Db::table('up_good_parameter')->delete($param_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 商品详情
     * @return mixed
     */
    public function goodsDetail(){
        $good_id = input('good_id');
        $goodInfo = Db::table('up_goods')
            ->where('good_id', $good_id)
            ->field('good_name, good_image, good_price, good_y_price, good_intro, 
            good_author, good_press, good_isbn, good_stock, good_content, good_type, good_yi')
            ->find();
        if($goodInfo['good_type'] == 1){//非图书
            $info = Db::table('up_goods')
                ->join('up_goods_assortment', 'assortment_id = good_assortment', 'left')
                ->where('good_id', $good_id)
                ->field('good_name, good_image, good_create_time, good_visitor, good_stock, 
                good_cost, good_price, assortment_name')
                ->find();
            //库存
            $good_stock = Db::table('up_good_spec')
                ->where('spec_good_id', $good_id)
                ->sum('spec_stock');
            $info['good_stock'] = $good_stock;
            //规格
            $spec = Db::table('up_good_spec')
                ->field('spec_price, spec_name')
                ->where('spec_good_id', $good_id)
                ->select();
            $info['spec'] = $spec;
            //图片
            if($info['good_image'] != ''){
                $info['good_image'] = explode(',', $info['good_image']);
            }else{
                $info['good_image'] = [];
            }
            $this->assign([
                'info' => $info
            ]);
            return $this->fetch('goods/unbookdetail');
        }else{//图书
            //图片
            if($goodInfo['good_image'] != ''){
                $goodInfo['good_image'] = explode(',', $goodInfo['good_image']);
            }else{
                $goodInfo['good_image'] = [];
            }
            $this->assign([
                'info' => $goodInfo
            ]);
            return $this->fetch('goods/bookdetail');
        }
                
    }
    
    /**
     * 编辑商品
     * @return mixed|\think\response\Json
     */
    public function goodsEdit(){
        $good_id = input('good_id');
        //商品信息
        $goodInfo = Db::table('up_goods')
            ->where('good_id', $good_id)
            ->field('good_name, good_thumb, good_y_price, good_intro, 
            good_author, good_press, good_isbn, good_stock, good_content, good_type, good_image,
            good_cost, good_assortment, good_weight, good_price, good_yi,good_grade_id,good_type_id,good_press_id')
            ->find();
        
        if(Request::instance()->isPost()){
            if($goodInfo['good_type'] == 1){//非图书
                $good_name = trim(input('good_name'), ' ');
                $name_len = mb_strlen($good_name, 'utf-8');
                if($name_len > 60){
                    return json(['code' => 100, 'msg' => '商品名不能超过60字']);
                }
                if(empty(input('photo'))){
                    return json(['code' => 100, 'msg' => '请上传图片']);
                }
    
//                $good_thumb = explode(',', input('photo'));
                $update['good_name'] = $good_name;
                $update['good_assortment'] = input('good_assortment');//分类;
                $update['good_cost'] =trim(input('good_cost'), ' ');//成本价
                $update['good_image'] = input('photo');//图片
                $update['good_update_time'] =  date('Y-m-d H:i:s');//编辑时间;
                $update['good_weight'] = trim(input('good_weight'), ' ');//重量;
                $update['good_content'] =  input('good_content');//图文详情;
                $update['good_thumb'] = input('param.img1');
                $update['good_grade_id'] = trim(input('good_grade_id'), ' ');//年级
                $update['good_type_id'] = trim(input('good_type_id'), ' ');//分类
                $update['good_press_id'] = trim(input('good_press_id'), ' ');//出版社

//                $update = [
//                    'good_name' => $good_name,//图书名称
//                    'good_assortment' => input('good_assortment'),//分类
//                    'good_cost' => trim(input('good_cost'), ' '),//成本价
//                    'good_thumb' => $good_thumb[0],//缩略图
//                    'good_image' => input('photo'),//图片
//                    'good_update_time' => date('Y-m-d H:i:s'),//编辑时间
//                    'good_weight' => trim(input('good_weight'), ' '),//重量
//                    'good_content' => input('good_content'),//图文详情
//                ];
                $res = Db::table('up_goods')->where('good_id', $good_id)->update($update);
                if($res){
                    return json(['code' => 200, 'msg' => '编辑成功']);
                }else{
                    return json(['code' => 100, 'msg' => '编辑成功']);
                }
            }else{//图书
                $good_name = trim(input('good_name'), ' ');
                $name_len = mb_strlen($good_name, 'utf-8');
                $good_price = trim(input('good_price'), ' ');
                if($name_len > 60){
                    return json(['code' => 100, 'msg' => '商品名不能超过60字']);
                }
                if(empty(input('photo'))){
                    return json(['code' => 100, 'msg' => '请上传图片']);
                }
                if(mb_strlen($good_price, 'utf-8') > 6){
                    return json(['code' => 100, 'msg' => '折扣价不可超过6个字']);
                }
                $good_thumb = explode(',', input('photo'));
                $update = [
                    'good_name' => trim(input('good_name'), ' '),//图书名称
                    'good_price' => $good_price,//折扣价
                    'good_update_time' => date('Y-m-d H:i:s'),//编辑时间
                    'good_y_price' => trim(input('good_y_price'), ' '),//金币
                    'good_author' => trim(input('good_author'), ' '),//作者
                    'good_press' => trim(input('good_press'), ' '),//出版社
                    'good_isbn' => trim(input('good_isbn'), ' '),//ISBN
                    'good_content' => input('good_content'),//图文详情
                    'good_intro' => trim(input('good_intro'), ' '),//简介
                    'good_weight' => input('good_weight'), //重量
                    'good_stock' => trim(input('good_stock'), ' '),//库存
                    'good_image' => input('photo'),//图片
                    'good_thumb' => input('param.img1'),//缩略图
                    'good_yi' => trim(input('good_yi'), ' '),//编译者
                     'good_grade_id' => trim(input('good_grade_id'), ' '),//年级
                     'good_type_id' => trim(input('good_type_id'), ' '),//分类
                     'good_press_id' => trim(input('good_press_id'), ' '),//出版社
                    'good_assortment' => input('good_assortment')//分类;

                ];
                $res = Db::table('up_goods')->where('good_id', $good_id)->update($update);
                if($res){
                    return json(['code' => 200, 'msg' => '编辑成功']);
                }else{
                    return json(['code' => 100, 'msg' => '编辑失败']);
                }
            }
        }
        
        //分类
//        $cate = Db::table('up_goods_assortment')
//            ->where('assortment_status', 1)
//            ->where('assortment_del', 1)
//            ->field('assortment_id, assortment_name')
//            ->select();
        
        if($goodInfo['good_type'] == 1){//非图书
            $cate = Db::table('up_goods_assortment')
                ->where('assortment_status', 1)
                ->where('assortment_del', 1)
                ->where('assortment_type', 1)
                ->field('assortment_id, assortment_name')
                ->select();
            $this->assign([
                'info' => $goodInfo,
                'cate' => $cate,
                'good_id' => $good_id
            ]);
            $data1 = Db::table('up_good_grade')->select();//年级
            $data2 = Db::table('up_good_press')->select();//出版社
            $data3 = Db::table('up_good_type')->select();//类型
            $this->assign('data1',$data1);
            $this->assign('data2',$data2);
            $this->assign('data3',$data3);
            return $this->fetch('goods/unbookedit');
        }else{
            $cate = Db::table('up_goods_assortment')
                ->where('assortment_status', 1)
                ->where('assortment_del', 1)
                ->where('assortment_type', 2)
                ->field('assortment_id, assortment_name')
                ->select();
            $this->assign([
                'info' => $goodInfo,
                'cate' => $cate,
                'good_id' => $good_id
            ]);
            $data1 = Db::table('up_good_grade')->select();//年级
            $data2 = Db::table('up_good_press')->select();//出版社
            $data3 = Db::table('up_good_type')->select();//类型
            $this->assign('data1',$data1);
            $this->assign('data2',$data2);
            $this->assign('data3',$data3);
            return $this->fetch('goods/bookedit');
        }
    }
    
    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function goodsStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择商品']);
        }
        $status = Db::table('up_goods')->where('good_id', 'IN', $ids)->column('good_status');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选商品中含有上架中商品，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选商品中含有已下架商品，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_goods')->where('good_id', 'IN', $ids)->setField('good_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 批量导出
     * @return \think\response\Json
     */
    public function excelGoods(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择商品']);
        }
        $where['good_id'] = array('IN',$ids);
        $lists = Db::name('goods')
            ->where($where)
            ->join('up_goods_assortment', 'assortment_id = good_assortment', 'left')
            ->field('good_id, good_name, good_price, good_create_time, good_status, 
            good_y_price, good_author, good_press, good_isbn, good_stock, assortment_name, 
            good_weight, good_cost, good_type, good_visitor, good_sales_num, good_intro')
            ->select();
        
        foreach($lists as $key => $vo){
            //状态
            if($vo['good_status'] == 1) $lists[$key]['good_status'] = '上架中';
            if($vo['good_status'] == 2) $lists[$key]['good_status'] = '已下架';
            
            $lists[$key]['spec'] = '';
            $lists[$key]['param'] = '';
            
            if($vo['good_type'] == 1){
                //规格
                $spec_arr = Db::table('up_good_spec')
                    ->field('spec_name, spec_stock, spec_price')
                    ->where('spec_good_id', $vo['good_id'])
                    ->select();
                foreach($spec_arr as $k => $v){
                    $spec = '规格名称:' . $v['spec_name'] . '、价格:' . $v['spec_price'] . '、库存:' . $v['spec_stock'];
                    $lists[$key]['spec'] .= $spec . ',';
                }
                $lists[$key]['spec'] = trim($lists[$key]['spec'], ',');
                
                //参数
                $param = Db::table('up_good_parameter')
                    ->field('parameter_name, parameter_value')
                    ->where('parameter_good_id', $vo['good_id'])
                    ->select();
                foreach($param as $kk => $vv){
                    $param = '参数名:' . $vv['parameter_name'] . '、参数值:' . $vv['parameter_value'];
                    $lists[$key]['param'] .= $param . ',';
                }
                $lists[$key]['param'] = trim($lists[$key]['param'], ',');
            }
            
            //类型
            if($vo['good_type'] == 1) $lists[$key]['good_type'] = '非图书';
            if($vo['good_type'] == 2) $lists[$key]['good_type'] = '图书';
            $vo['good_isbn'] =  "\t".$vo['good_isbn']."\t";
            $vo['good_visitor'] =  "\t".$vo['good_visitor']."\t";
        }
      
        $cellname = [
            ['good_id','商品ID',10,'LEFT'],
            ['good_name','商品名称',40,'LEFT'],
            ['good_create_time','上架时间',20,'LEFT'],
            ['good_price','折扣价（图书）',20,'LEFT'],
            ['good_y_price','金币价格（图书）',20,'LEFT'],
            ['good_cost','成本价（非图书）',20,'LEFT'],
            ['good_author','作者',10,'LEFT'],
            ['good_press','出版社',15,'LEFT'],
            ['good_isbn','ISBN',15,'LEFT'],
            ['good_stock','库存（图书）',20,'LEFT'],
            ['spec','规格（非图书）',40,'LEFT'],
            ['param','参数（非图书）',40,'LEFT'],
            ['assortment_name','类目',12,'LEFT'],
            ['good_weight','重量（Kg）',12,'LEFT'],
            ['good_type','分类',8,'LEFT'],
            ['good_visitor','访客数',8,'LEFT'],
            ['good_sales_num','销量',8,'LEFT'],
            ['good_intro','简介',30,'LEFT'],
        ];
        $res = exportExcel('平台商品列表','Goods',$cellname,$lists);
        return json($res);
    }
    
    /**
     * 删除队列中的图片
     * @return \think\response\Json
     */
    public function deleteImg(){
        $add = input('add');
        $url =  http_type();
        $add = str_replace ($url,'',$add);
//        $add = ltrim($add,  '/');
        $add = ROOT_PATH . 'public' . $add;
        if(unlink($add)){
            return json(['code'=>200,'msg'=>'删除成功！']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败！']);
        }
    }
    //推荐
    public function recommend(){
        $id = input('param.id');
        $data = Db::table('up_goods')->where('good_id',$id)->find();
        if($data['good_hot']==1){
            $res = Db::table('up_goods')->where('good_id',$id)->setField('good_hot',2);
        }elseif ($data['good_hot']==2){
            $res = Db::table('up_goods')->where('good_id',$id)->setField('good_hot',1);
        }
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}